package com.ywm.service.impl;

import cn.hutool.core.date.DateTime;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ywm.config.AliyunOssConfig;
import com.ywm.dto.QueryBlog;
import com.ywm.entity.Blog;
import com.ywm.mapper.BlogMapper;
import com.ywm.service.BlogSrevice;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ywm.entity.PageBlog;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;


@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements BlogSrevice {
    @Autowired
    BlogMapper blogMapper;
    @Override
    public PageBlog getBlogByPage(int page, int size,QueryBlog queryBlog) {
        //分页查询条件
        IPage<Blog> blogIPage = new Page<Blog>();
        blogIPage.setCurrent(page);
        // blogIPage.setPages(page);
        blogIPage.setSize(size);
        //查询条件
        QueryWrapper<Blog> queryWrapper = new QueryWrapper<Blog>();
        if(queryBlog != null){
            if(!StringUtils.isEmpty(queryBlog.getBlogTitle())){
                queryWrapper.eq("blog_title",queryBlog.getBlogTitle());
            }
            if(!StringUtils.isEmpty(queryBlog.getBlogType())){
                queryWrapper.eq("blog_type",queryBlog.getBlogType());
            }
            if(!StringUtils.isEmpty(queryBlog.getAuthor())){
                queryWrapper.eq("author",queryBlog.getAuthor());
            }
        }
        queryWrapper.orderByDesc("blog_read");
        //查询出数据
        IPage<Blog> page1 = blogMapper.selectPage(blogIPage,queryWrapper);
        //将数据封装在PageBlog对象
        PageBlog pageBlog = new PageBlog();
        pageBlog.setBlog(page1.getRecords());
        pageBlog.setCurrentPage( page1.getCurrent());
        pageBlog.setPageSize(page1.getPages());
        pageBlog.setSize(page1.getSize());
        pageBlog.setTotalSize(page1.getTotal());
        return pageBlog;
    }

    @Override
    public PageBlog getBlogByPageAndAuthor(int page, int size, QueryBlog queryBlog) {
        //分页查询条件
        IPage<Blog> blogIPage = new Page<Blog>();
        blogIPage.setCurrent(page);
        blogIPage.setSize(size);
        //查询条件
        QueryWrapper<Blog> queryWrapper = null;
        if(queryBlog != null){
            queryWrapper = new QueryWrapper<Blog>();
            queryWrapper.eq("author",queryBlog.getAuthor());
            if(!StringUtils.isEmpty(queryBlog.getBlogTitle())){
                queryWrapper.eq("blog_title",queryBlog.getBlogTitle());
            }
            if(!StringUtils.isEmpty(queryBlog.getBlogType())){
                queryWrapper.eq("blog_type",queryBlog.getBlogType());
            }
        }
        //查询出数据
        IPage<Blog> page1 = blogMapper.selectPage(blogIPage,queryWrapper);
        //将数据封装在PageBlog对象
        PageBlog pageBlog = new PageBlog();
        Blog blog = new Blog();
        pageBlog.setBlog(page1.getRecords());
        pageBlog.setCurrentPage( page1.getCurrent());
        pageBlog.setPageSize(page1.getPages());
        pageBlog.setSize(page1.getSize());
        pageBlog.setTotalSize(page1.getTotal());
        return pageBlog;
    }

    @Override
    public String uploadFileAvatar(MultipartFile file) {
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        String endpoint = AliyunOssConfig.JAVA_END_POINT;
        // 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建。
        String accessKeyId = AliyunOssConfig.JAVA_ACCESS_KEY_ID;
        String accessKeySecret = AliyunOssConfig.JAVA_ACCESS_KEY_SECRET;
        String bucketName=AliyunOssConfig.JAVA_BUCKET_NAME;
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        // 上传文件流。
        InputStream inputStream = null;
        //文件名称
        String name= UUID.randomUUID().toString().replaceAll("-","");
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //将文件安装日期进行分类
        //2020/05/16/01.jpg
        //获取当前日期
        //String format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
        String datePath= new DateTime().toString("yyyy/MM/dd");
        name=datePath+"/"+name;
//        System.out.println(name);
        /**
         * 参数一：bucket的名称
         * 参数二：上传到OSS文件的路径和名称 /aa/bb/1.jpg
         * 参数三：上传文件的输入流
         */
        ossClient.putObject(bucketName, name/*获得文件原始名称*/, inputStream);
        try {
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 关闭OSSClient。
        ossClient.shutdown();
        //把上传之后文件路径返回
        //需要把上传到阿里云oss路径手动拼接出来
        //https://ywm-guli.oss-cn-heyuan.aliyuncs.com/1.jpg
        return "https://"+bucketName+"."+endpoint+"/"+name;
    }
}
